/*
package com.beone.smart.smartBeauty.config;

import com.beone.smart.smartBeauty.model.result.ResultEntity;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

*/
/**
 * @Date: 2019/9/4
 * @Author: yangjie
 * desc:
 **//*

@ControllerAdvice
public class ApiResponseBody implements ResponseBodyAdvice<Object> {

    @ExceptionHandler(Exception.class)
    @ResponseBody //在返回自定义相应类的情况下必须有，这是@ControllerAdvice注解的规定
    public ResultEntity exceptionHandler(Exception exception) {
        exception.printStackTrace();
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setStatus(400);
        resultEntity.setMsg(getRealMessage(exception));
        return ResultTool.fail(resultEntity);
    }

    @ExceptionHandler(NotFoundException.class)
    public ResultEntity notFoundPage404(Exception exception) {
        exception.printStackTrace();
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setStatus(404);
        resultEntity.setMsg(exception.getMessage());
        return ResultTool.fail(resultEntity);
    }

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object object,
                                  MethodParameter methodParameter,
                                  MediaType mediaType, Class aClass,
                                  ServerHttpRequest serverHttpRequest,
                                  ServerHttpResponse serverHttpResponse) {
        if (object instanceof ResultEntity) {
            return object;
        } else {
            return ResultTool.ok(object);
        }
    }

    // 获取嵌套异常中的最终信息
    private String getRealMessage(Throwable e) {
        // 如果e不为空，则去掉外层的异常包装
        while (e != null) {
            Throwable cause = e.getCause();
            if (cause == null) {
                return e.getMessage();
            }
            e = cause;
        }
        return "";
    }

}
*/
